package org.gl.training.gwt.client.view;

import org.gl.training.gwt.client.service.UserService;
import org.gl.training.gwt.client.service.UserServiceAsync;
import org.gl.training.gwt.client.utils.MyAsyncCallback;
import org.gl.training.gwt.shared.model.UserVO;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class InfoView extends Window {

	private UserServiceAsync userService = GWT.create(UserService.class);

	private TextItem userNameItem;

	private TextItem fullNameItem;

	private TextItem emailItem;

	private TextItem notesItem;

	public InfoView(String userName) {
		super();
		setMembersMargin(15);
		setWidth(300);
		setHeight(200);
		setTitle("Info");
		setShowMinimizeButton(false);
		setShowCloseButton(false);
		setIsModal(true);
		setShowModalMask(true);
		centerInPage();

		VLayout layout = new VLayout(10);

		DynamicForm form = new DynamicForm();
		form.setErrorOrientation(FormErrorOrientation.RIGHT);
		form.setNumCols(3);
		form.setColWidths("54%", "5%", "41%");
		form.setLayoutAlign(Alignment.LEFT);

		userNameItem = new TextItem();
		userNameItem.setTitle("User Name");
		userNameItem.setAttribute("readOnly", true);
		
		fullNameItem = new TextItem();
		fullNameItem.setTitle("Full Name");
		fullNameItem.setAttribute("readOnly", true);
	
		emailItem = new TextItem();
		emailItem.setTitle("Email");
		emailItem.setAttribute("readOnly", true);
		
		notesItem = new TextItem();
		notesItem.setTitle("Notes");
		notesItem.setAttribute("readOnly", true);

		form.setFields(new FormItem[] { userNameItem, fullNameItem, emailItem,
				notesItem });

		HLayout hLayout = new HLayout(10);

		Button cancelButton = new Button("Cancel");
		cancelButton.addClickHandler(new CancelHandler());

		hLayout.addMember(cancelButton);
		hLayout.setAlign(Alignment.CENTER);

		layout.addMember(form);
		layout.addMember(hLayout);

		addItem(layout);

		loadUserData(userName);
	}

	protected void loadUserData(String userName) {
		userService.getUser(userName, new LoadUserCallback());
	}

	private class LoadUserCallback extends MyAsyncCallback<UserVO> {

		@Override
		public void onSuccess(UserVO userVO) {
			userNameItem.setValue(userVO.getUserName());
			fullNameItem.setValue(userVO.getFullName());
			emailItem.setValue(userVO.getEmail());
			notesItem.setValue(userVO.getNotes());
		}
	}

	private class CancelHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			hide();
		}
	}
}
